
#include "hw_config.h"
#include "ds1302.h"
#include "uart.h"

/*******************************************
Function name: ds1302_display_time
Feature: 12864 shows the current time (line 1 format: year - month - day week; 2 line format: time - minutes - seconds)
Parameters: time [] - array of time
Return Value: None
********************************************/
void ds1302_display_time(byte set_time[])
{
	byte asc[2];
	byte line1[11] = {0,0 ,'-', 0,0 ,'-', 0,0, ' ', 0, '\0'}; // display an array of characters on line 1
	byte line2[9] = {0,0 ,':', 0,0 ,':', 0,0, '\0'};  // display an array of characters in line 2

	bcd2ascii (set_time[3], asc); // time
	line2[0] = asc[0];
	line2[1] = asc[1];
	bcd2ascii (set_time[4], asc); // min
	line2[3] = asc[0];
	line2[4] = asc[1];
	bcd2ascii (set_time[5], asc); // sec
	line2[6] = asc[0];
	line2[7] = asc[1];

	bcd2ascii (set_time[0], asc); // for the assignment in line 1
	line1[0] = asc[0];
	line1[1] = asc[1];
	bcd2ascii (set_time[1], asc); // for the assignment on line 1
	line1[3] = asc[0];
	line1[4] = asc[1];
	bcd2ascii (set_time[2], asc); // for the assignment on line 1
	line1[6] = asc[0];
	line1[7] = asc[1];
	bcd2ascii (set_time[6], asc); // line 1 week for the first assignment
	line1[9] = asc[1];

	usart0_format_puts("20%s %s\r\n", line1, line2);

}

void main(void)
{

	byte get_time [7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // array to save the current time
	byte cur_time [7] = {0x55, 0x14, 0x10, 0x19, 0x06, 0x05, 0x09}; // seconds, minutes, hours, days, months, weeks, years

	CLI();

	bsp_usart0_gpio_init();

	init_usart0_buffer();
	if( F_CPU == 8000000UL ) 	
	{
		bsp_usart0_init(F_CPU, EBaud38400, EData8, EParNone, EStop1, FALSE );
	}
	else
	{
		bsp_usart0_init(F_CPU, EBaud115200, EData8, EParNone, EStop1, FALSE );	
	}
	
	//register_uart0_function(uart0_rx_event);
	bsp_usart0_interrupt_enable();

	SEI();	// all interrupt enable

	bsp_ds1302_gpio_init();

	_delay_ms (500);

	ds1302_set_time(cur_time);

	// year-month-days week time-minute-seconds
	usart0_puts("\r\n Set 2009-06-19 5 10:14:56 \r\n");


	while(1)
	{
		ds1302_read_time(get_time);
		ds1302_display_time(get_time);
		_delay_ms(1000);
		
	}

}
